<template>
  <div class="primary">
    <PersonalHeader :content="1"></PersonalHeader>

    <section class="primarymain">
      <section class="type">
        <h2 class="title"> {{$t('gaizj[0]')}}
          <p>{{$t('gaizj[1]')}}</p>
        </h2>
        <ul class="contt">
          <el-form label-position="top" label-width="80px" :model="formLabelAlign" :rules="rules" ref="ruleForm" prop="name">
            <!-- <el-form-item :label="$t('gaizj[2]')"  prop="old_pay_pwd">
                            <el-input v-model="formLabelAlign.old_pay_pwd" :placeholder="$t('zijin[3]')" show-password></el-input>
                        </el-form-item> -->
            <el-form-item :label="$t('zijin[2]')" prop="password">
              <el-input v-model="formLabelAlign.new_pay_pwd" :placeholder="$t('zijin[3]')" show-password></el-input>
            </el-form-item>
            <el-form-item :label="$t('mobile[6]')" prop="new_pay_pwd_confirmation">
              <el-input v-model="formLabelAlign.new_pay_pwd_confirmation" :placeholder="$t('zijin[4]')" show-password></el-input>
            </el-form-item>
            <div class="tips">
              <p>我们将向如下手机发送一组验证码</p>
              <p>{{Hidephone($store.state.user.phone)}}</p>
            </div>
            <el-form-item :label="$t('mobile[9]')" prop="v_code">
              <el-input v-model="formLabelAlign.v_code" :placeholder="$t('zijin[5]')">
                <template slot="append">
                  <el-button @click="Verificationfun()">{{Verification.text}}</el-button>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSubmit" :loading="loading">{{$t('senior[8]')}}</el-button>
            </el-form-item>
          </el-form>
        </ul>
      </section>
    </section>
  </div>
</template>

<script>
import { resetPaymentPassword } from '@/api/securitycenter'
import PersonalHeader from "_dom/personalheader";
import { Passwordencryption, EmailSms, Hidephone } from "@/libs/util";
export default {
  name: 'capitalpassword',
  data() {
    var validatePass1 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('gaizj[3]')));
      } else if (this.$public.pwds(value)) {
        callback();
      } else {
        callback(new Error(this.$t('reg[13]')));
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('gaizj[4]')));
      } else if (this.$public.pwds(value)) {
        callback();
      } else {
        callback(new Error(this.$t('reg[13]')));
      }
    };
    var validatePass3 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('gaizj[5]')));
      } else if (value !== this.formLabelAlign.new_pay_pwd) {
        callback(new Error(this.$t('gaizj[6]')));
      } else {
        callback();
      }
    };
    return {
      formLabelAlign: {
        // old_pay_pwd:"",
        new_pay_pwd: "",
        new_pay_pwd_confirmation: "",
        v_code: "",
      },
      Verification: {
        text: this.$t('reg[0]'),
        time: 60,
        type: true,
        timer: "",
      },
      loading: false,
      rules: {
        // old_pay_pwd: [
        //     { validator: validatePass1, trigger: 'blur' }
        // ],
        new_pay_pwd: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        new_pay_pwd_confirmation: [
          { validator: validatePass3, trigger: 'blur' }
        ],
        v_code: [
          // { required: false, message: this.$t('mobile[15]'), trigger: 'change' }
        ],
      },
    }
  },
  methods: {
    Hidephone(data) {
      return Hidephone(data)
    },
    handleSubmit() {//修改资金密码
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          resetPaymentPassword(Passwordencryption(this.formLabelAlign)).then(res => {
            this.loading = false;
            this.$message({
              message: this.$t('gaizj[7]'),
              type: 'success'
            });
            setTimeout(() => {
              this.$router.push({ path: "/securitycenter" });
            }, 800);
          }).catch(err => {

          });
        }
      })
    },
    Verificationfun() {
      if (!this.Verification.type) {
        return false;
      }
      var _data = {
        type: "2",
        phone: this.$store.state.user.phone ? this.$store.state.user.phone : this.$store.state.user.email,

      };
      this.$public.captcha(_data, this);
    },
    typecaptcha() {
      this.Verification.type = false;
      this.Verification.timer = setInterval(() => {
        this.Verification.time--;
        if (this.Verification.time < 1) {
          clearInterval(this.Verification.timer);
          this.Verification.type = true;
          this.Verification.time = 60;
          this.Verification.text = this.$t('reg[1]');
        } else {
          this.Verification.text = this.Verification.time + "S";
        }
      }, 1000);
    }
  },
  created() {

  },
  beforeEach: function (to, from, next) {
    clearInterval(this.Verification.timer);
    next();
  },
  components: {
    PersonalHeader
  },
}
</script>

